package pps.et5.eit.ex1;
import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory; 
import javax.jms.DeliveryMode; 
import javax.jms.Destination;
import javax.jms.MessageProducer; 
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
public class Producer {
	private ConnectionFactory connectionFactory; 
	private Destination destination;
	public static void main(String[] args) {
		try {
			Producer producer = new Producer();
			producer.connect();
			producer.sendMessages();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
	private void sendMessages() throws Exception {
		// Créer une connexion au système de messagerie
		// Emet des messages au fur et à mesure que l’utilisateur les saisit
		
		// Création d'une Connexion et d'une Session:
		Connection connection = connectionFactory.createConnection(); 
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// Création d'un MessageProducer et d'un message de type Text
		MessageProducer producer = session.createProducer(destination); 
		while(true){
			String smes;
			Scanner in = new Scanner(System.in);
			// Reads a single line from the console 
			// and stores into name variable
			smes = in.nextLine();
			if(smes.compareTo("stop")==0){
				break;
			}
			TextMessage message = session.createTextMessage(smes);
			// Envoi d'un message
			producer.send(message);
		}
		// Fermeture de la connexion
		connection.close();
	}

	private void connect() throws Exception {
		// Initialise les attributs connectionFactory et destination.
		// Création d'un contexte JNDI
		InitialContext jndiContext = new InitialContext();
		// Lookup de la fabrique de connexion et de la destination
		connectionFactory = (ConnectionFactory) jndiContext.lookup("connectionFactory"); 
		destination = (Destination) jndiContext.lookup("Request");
	}
}
